package com.icehelper.hades.base.exception;

/**
 * 公共错误码。 <br/><br/>
 *
 * @author Leon Sun
 */
public enum CommonErrorCode implements IErrorCode{
	SUCCESS( 0, "成功" ),

	// 9000 - 9999 处理中
	PROCESSING( 9000, "处理中" ),
	NO_RESPONSE( 9001, "无返回" ),

	// 10000 - 19999 成功
	DUPLICATE( 10001, "重复操作" ),
	NO_QUERY_RESULT( 10002, "查询结果为空" ),

	// 20000 - 29999 基础校验失败（非业务）
	SERVER_RATE_LIMIT( 20002, "啊哦，当前人数较多，请稍后再试~" ),
	NOT_FOUND_ERROR( 20404, "数据不存在" ),
	SIGN_VERIFY_FAIL( 22000, "验签失败" ),

	PARAM_ERROR( 21000, "参数错误" ),
	PARAM_MISSING( 21001, "缺少参数" ),
	PARAM_INVALID( 21002, "参数无效" ),

	BUSINESS_ERROR( 29999, "业务错误" ),

	// 90000 - 99999 系统错误
	SERVER_ERROR( 90000, "系统繁忙，请稍后重试" ),
	SERVER_BUSY( 90000, "系统繁忙，请稍后重试" );

	private int code;
	private String desc;

	CommonErrorCode( int code, String desc ){

		this.code = code;
		this.desc = desc;
	}

	@Override
	public int code(){

		return this.code;
	}

	public CommonErrorCode code( Integer code ){

		this.code = code;
		return this;
	}

	@Override
	public String msg(){

		return this.desc;
	}

	public boolean checkCode( Integer code ){

		return code != null && code == this.code;
	}

	public boolean checkCode( CommonErrorCode bec ){

		return bec != null && bec.code() == this.code;
	}

	@Override
	public String toString(){

		return "ErrorCode{" + "code=" + code + ", desc='" + desc + '\'' + '}';
	}

	public static CommonErrorCode codeOf( Integer code ){

		if( code == null ){
			return null;
		}
		for( CommonErrorCode bec : CommonErrorCode.values() ){
			if( bec.checkCode( code ) ){
				return bec;
			}
		}
		return null;
	}

}
